using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BHXH.Core.Domain;
using BHXH.Core.IService;
using FX.Data;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;

namespace BHXH.Core.ServiceImp
{
    public class hs_D01bTS_msService: BaseService<hs_D01bTS_ms ,int>,Ihs_D01bTS_msService
    {
        static ISession mySession;
        public hs_D01bTS_msService(string sessionFactoryConfigPath)
            : base(sessionFactoryConfigPath)
        {}

        public hs_D01bTS_ms Load_ThongTinHoSo(int ID, String IDref)
        {
            hs_D01bTS_ms tt_dk_hsdv = new hs_D01bTS_ms();
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                IList<hs_D01bTS_ms> query = mySession.CreateQuery("FROM hs_D01bTS_ms as tt_hsdv where tt_hsdv.IDRef = :IDRef ")
                    .SetParameter("IDRef", IDref)
                    .List<hs_D01bTS_ms>();

                if (query.Count > 0)
                {
                    tt_dk_hsdv = query[0];
                    return tt_dk_hsdv;
                }
                else
                {
                    //chua dang ki, lay ra tu bang khac va insert 
                    IList<thongtincongty> query_thongtincongty = mySession.CreateQuery("FROM thongtincongty as ttct where ttct.ID = :id ")
                    .SetParameter("id", ID)
                    .List<thongtincongty>();
                    thongtincongty tthsdkbh = new thongtincongty();

                    tthsdkbh = query_thongtincongty[0];

                    tt_dk_hsdv.KinhGui = tthsdkbh.QuanHuyen;
                    tt_dk_hsdv.TenDV = tthsdkbh.TenCongTy;
                    tt_dk_hsdv.MSTQL = tthsdkbh.MaSoThue;
                    tt_dk_hsdv.DiaChi = tthsdkbh.DiaChi;
                    return tt_dk_hsdv;                  
                }
                
            }
        }

        public void UpDate_ThongTinHoSo(hs_D01bTS_ms ttdk)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                mySession.Update(ttdk);
                mySession.Flush();
            }
        }

        public void Add_ThongTinHoSo(hs_D01bTS_ms ttdk)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                CreateNew(ttdk);
                CommitChanges();
            }
        }     
    }
    
}